#pragma once
#include <cstring>
#include <map>

#pragma warning(disable:4996)

class LOG
{
private:
    std::string strLogName;
    std::string strFuncName;
    std::map<std::string,std::string> mParamInfo;
    template<class T>
    std::string param2Str(T param);
public:
    LOG() { strLogName = ""; strFuncName = ""; };
    LOG(char *);
    LOG(char *, char *);
    LOG operator=(const LOG);
    LOG operator+=(const LOG);
    /*
    * add function params
    * first param is param name
    * second param is param data
    */
    template<class t>
    int addparam(char *, t);
    /*
    * set Log file name
    * param : Logname data
    */
    int setLogName(char *);
    /*
    * set Log function name
    * param : function name data
    */
    int setFuncName(char *);
    /*
    * write Log
    */
    int logWrite();
    /*
    * get function params map
    */
    std::map<std::string, std::string> getParams();
    ~LOG();
};
//make params to string and show HEX
template<class T>
inline std::string LOG::param2Str(T param)
{
    size_t stPrameSize = sizeof(param) * 2;
    char *pParam = new char[stPrameSize];
    memset(pParam, 0x00, stPrameSize);
    for (int i = 0; i < sizeof(param); ++i)
        sprintf(pParam + i * 2, "%02x", ((char *)&param)[i]);
    std::string strParam = std::string(pParam);
    delete []pParam;
    pParam = NULL;
    return strParam;
}
//add params to Log class
template<class t>
inline int LOG::addparam(char *pParamName, t param)
{
    if (NULL == pParamName || 0 == strlen(pParamName))
        return 0;
    std::string strParamName = std::string(pParamName);
    std::string strParam = param2Str(param);
    mParamInfo.insert(std::pair<std::string, std::string>(strParamName, strParam));
    return 1;
}
